package com.sali.service.data_processor.chain;

import com.sali.commons.exception.ProcessChainHandlerInitException;
import com.sali.entity.ProcessDefinition;
import com.sali.service.data_processor.chain.handlers.DataHandler01;
import com.sali.service.data_processor.chain.handlers.DataHandler02;
import com.sali.service.data_processor.chain.handlers.DataHandler03;
import com.sali.service.statistic.PerformanceStatisticProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProcessChainHandlerFactory {

    private static Logger log = LoggerFactory.getLogger(ProcessChainHandlerFactory.class);

    private ProcessChainHandlerFactory() {
        throw new RuntimeException("无需创建实例！！！");
    }

    // 获取 ProcessChainHandler,并初始化
    public static IProcessChainHandler createInstance(int code, ProcessDefinition config) {
        IProcessChainHandler handler = null;
        switch (code) {
            case 1:
                handler = new DataHandler01();
                break;
            case 2:
                handler = new DataHandler02();
                break;
            case 3:
                handler = new DataHandler03();
                break;
            default:
                break;
        }
        if (handler != null) {
            try {
                handler.preProcess(config);
            } catch (ProcessChainHandlerInitException e) {
                log.error("[初始化] 初始化handler：{}异常，请检查配置文件", handler.getClass().getName(), e);
                throw new RuntimeException();
            }
        }

        // 性能统计代理
        handler = new PerformanceStatisticProxy(handler);

        return handler;
    }


}
